﻿var StatusData = [{ id: 0, text: '新订单' }, { id: 1, text: '成功' }, { id: 2, text: '失败'}];
mini.parse();
$(document).ready(function() {
    $.ajax({
        url: "../Service/SystemManageHandler.ashx?action=CheckPermissions",
        cache: false,
        success: function(data) {
            var remove = mini.get("remove");
            remove.setEnabled(false);
            if (data == "1") {
                remove.setEnabled(true);
            }
        }
    });
});

var now = new Date();
var nowYear = now.getFullYear();
var nowMonth = now.getMonth();
var StartDateTime = mini.get("StartDateTime");
StartDateTime.setValue(new Date(nowYear, nowMonth, 1));
var EndDateTime = mini.get("EndDateTime");
EndDateTime.setValue(new Date());
var grid = mini.get("grid1");

function add() {
    mini.open({
        url: "SystemManage/OrderInfoForm.aspx",
        title: "新增", width: 470, height: 180,
        onload: function() {
            var iframe = this.getIFrameEl();
            var data = { action: "new" };
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function(action) {
            grid.reload();
            GetTotalAmount(mini.get("StartDateTime").getFormValue(), mini.get("EndDateTime").getFormValue(), mini.get("Mobile").getValue(), mini.get("Status").getValue());
        }
    });
}

function remove() {
    var row = grid.getSelected();
    if (row) {
        mini.confirm("您确定要删除:" + row.Mobile + "？", "确定？",
                    function(action) {
                        if (action == "ok") {
                            $.ajax({
                                url: "../Service/SystemManageHandler.ashx?action=RemoveOrder&OrderId=" + row.OrderId,
                                success: function(text) {
                                    grid.reload();
                                    GetTotalAmount(mini.get("StartDateTime").getFormValue(), mini.get("EndDateTime").getFormValue(), mini.get("Mobile").getValue(), mini.get("Status").getValue());
                                },
                                error: function(jqXHR, textStatus, errorThrown) {
                                    alert(jqXHR.responseText);
                                }
                            });
                        } else {
                            return false;
                        }
                    });

    } else {
        mini.alert("请选中一条记录!");
    }
}

function search(e) {
    var StartDateTime = mini.get("StartDateTime").getFormValue();
    var EndDateTime = mini.get("EndDateTime").getFormValue();
    var Mobile = mini.get("Mobile").getValue();
    var Status = mini.get("Status").getValue();
    grid.setUrl("../service/SystemManageHandler.ashx?action=GetOrderList");
    grid.load({ StartDateTime: StartDateTime, EndDateTime: EndDateTime, Mobile: Mobile, Status: Status });
    GetTotalAmount(StartDateTime, EndDateTime, Mobile, Status);

}

function GetTotalAmount(StartDateTime, EndDateTime, Mobile, Status) {
    $.ajax({
        url: "../Service/SystemManageHandler.ashx?action=GetTotalAmount&StartDateTime=" + StartDateTime + "&EndDateTime=" + EndDateTime + "&Mobile=" + Mobile + "&Status=" + Status,
        cache: false,
        success: function(data) {
            var o = mini.decode(data);
            $("#TotalSuccessAmount").text(o.TotalSuccessAmount);
            $("#TotalFailureAmount").text(o.TotalFailureAmount);
            $("#TotalNewAmount").text(o.TotalNewAmount);
        }
    });
}

function onKeyEnter(e) {
    search();
}
function reset(e) {
    StartDateTime.setValue(new Date(nowYear, nowMonth, 1));
    EndDateTime.setValue(new Date());
    mini.get("Mobile").setValue("");
    mini.get("Status").setValue("");
}
function onStatusRenderer(e) {
    if (e.value == "2") { return "<span style=\"color: Red\">失败</span>"; }
    else if (e.value == "1") { return "<span style=\"color: Green\">成功</span>"; }
    else if (e.value == "0") { return "<span style=\"color: blue\">新订单</span>"; }
    else { return ""; }
}

function CloseWindow(action) {
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function close() {
    CloseWindow("close");
}